﻿using System;
using System.IO;
using SharpSvn;
using System.Reflection;
using System.Diagnostics;

namespace RevStamp {
    class Program {
        static void Main(string[] args) {
            if (args.Length != 3) {
                PrintHelpMessage();
                return;
            }
            try {
                rev(args[0], args[1], args[2]);
            }
            catch (Exception e) {
                Console.Write(e);
            }
        }

        static void PrintHelpMessage() {
            Console.Write("\nREVSTAMP is a tool to create a text file from a template with SVN revision\n");
            Console.Write("information replacing predefined variables in the template file.\n\n\n");
            Console.Write("Usage:\n\n");
            Console.Write("  REVSTAMP.EXE [svnurl] [template] [output]\n\n");
            Console.Write("    svnurl     The URL to the SVN repository the latest revision number\n");
            Console.Write("               should be retrieved from.\n\n");
            Console.Write("    template   File to be used as a template for file generation. All\n");
            Console.Write("               instances of ${revision} will be replaced with the latest\n");
            Console.Write("               revison number of the repository located at [svnurl].\n\n");
            Console.Write("    output     The file to be writen based on the [template] file with\n");
            Console.Write("               SVN string replacements applied.\n\n\n");
            Console.Write("Example:\n\n");
            Console.Write("  REVSTAMP.EXE http://myapp.com/svn/trunk tmpl.txt AssemblyInfo.cs\n");
        }



        static void rev(string url, string templatePath, string outputPath) {
            StreamReader templateFile;
            StreamWriter outputFile;
            SvnInfoEventArgs svnInfo;

            // attempt to open template file
            try { templateFile = new StreamReader(templatePath); }
            catch (Exception e) {
                Console.WriteLine("Error opening template file. {0}", e.Message);
                return;
            }

            // attempt to open output file
            try { outputFile = new StreamWriter(outputPath, false); }
            catch (Exception e) {
                Console.WriteLine("Error opening output file. {0}", e.Message);
                return;
            }

            // grab svn info
            try { svnInfo = GetSvnInfo(url); }
            catch (Exception e) {
                Console.WriteLine("Could not connect to SVN repository. {0}", e.Message);
                return;
            }

            // replace revision
            string text = templateFile.ReadToEnd();
            text = text.Replace("${revision}", svnInfo.Revision.ToString());

            // write and save the file
            outputFile.Write(text);
            outputFile.Close();
        }

        static SvnInfoEventArgs GetSvnInfo(string url) {
            SvnClient client = new SvnClient();
            SvnTarget target = new SvnUriTarget(url);
            SvnInfoEventArgs info;

            client.GetInfo(target, out info);
            return info;
        }

    }
}
